package sfeir.ergosum.dao;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.commons.io.IOUtils;

import sfeir.ergosum.model.Contact;
import sfeir.ergosum.model.Person;
import sfeir.ergosum.tools.ErgoSumParser;
import sfeir.ergosum.tools.MicroformatDomParser;
import sfeir.ergosum.tools.SpreadsheetParser;
import sfeir.ergosum.tools.VCardParser;
import android.net.Uri;

public class RemoteProvider {
    private final DatabaseProvider db;

    public RemoteProvider(DatabaseConnection dbCo) {
        db = new DatabaseProvider(dbCo);
    }

    /**
     * Add a new contact to the database by loading the contact from the
     * spreadsheet data
     * 
     * @param url
     *            the spreadsheet key
     * @return The new contact created
     */
    public Contact addContactBySpreadsheet(String url, String alias) {
        db.open();
        Contact contact = db.getContactByURL(url);
        if (contact == null) {
            contact = new Contact(url);
            SpreadsheetParser sp = new SpreadsheetParser(url);
            Person person = sp.getPerson();
            if (person == null) {
                db.close();
                return null;
            }
            contact.setPerson(person);
            if (alias.equals(""))
                contact
                        .setAlias(person.getFirstname() + " "
                                + person.getName());
            else
                contact.setAlias(alias);
            long l = db.insertContact(contact);
            contact.setId(l);
        }
        db.close();
        return contact;
    }

    /**
     * Add a new contact to the database by loading the contact from the
     * spreadsheet data
     * 
     * @param url
     *            the spreadsheet key
     * @return The new contact created
     */
    public Contact addContactByWebsite(String url) {
        db.open();
        Contact contact = db.getContactByURL(url);
        if (contact == null) {
            contact = new Contact(url);
            MicroformatDomParser sp = new MicroformatDomParser(url);
            Person person = sp.getPerson();
            if (person == null) {
                db.close();
                return null;
            }
            person.setUrl(url);
            contact.setPerson(person);
            contact.setAlias(person.getFirstname() + " " + person.getName());
            long l = db.insertContact(contact);
            contact.setId(l);
        }
        db.close();
        return contact;
    }
    
    /**
     * Add a new contact to the database by loading the contact from the
     * service ErgoSum
     * 
     * @param url
     *            the spreadsheet key
     * @return The new contact created
     */
    public Contact addContactByErgoSum(String url) {
        db.open();
        Contact contact = db.getContactByURL(url);
        if (contact == null) {
            
            ErgoSumParser sp = new ErgoSumParser(url);
            Person person = sp.getPerson();
            if (person == null) {
                db.close();
                return null;
            }
            contact = new Contact(sp.getUrl());
            person.setUrl(sp.getUrl());
            contact.setPerson(person);
            contact.setAlias(person.getFirstname() + " " + person.getName());
            long l = db.insertContact(contact);
            contact.setId(l);
        }
        db.close();
        return contact;
    }

    public Contact addContactByFile(Uri url) {
        db.open();
        Contact contact = db.getContactByURL(url.toString());
        Person person = getVcardFile(url.toString());
        if (person == null) {
            db.close();
            return null;
        }
        contact = new Contact(url.toString());
        contact.setPerson(person);
        contact.setAlias(person.getFullName());
        long l = db.insertContact(contact);
        contact.setId(l);
        db.close();
        return contact;
    }

    public static Person getVcardFile(String url) {
        Person person = null;
        try {
            FileInputStream input = new FileInputStream(url
                    .replace("file:", ""));
            String vcard = IOUtils.toString(input);
            person = VCardParser.parse(vcard);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return person;
    }

    public static Person getUpdatedPerson(String url) {
        if (url.matches("(\\w|-)+")) {
            SpreadsheetParser sp = new SpreadsheetParser(url);
            return sp.getPerson();
        } else if (!url.startsWith("content:")) {
            // TODO Update from the phone
            return null;
        } else if (url.contains(ErgoSumParser.URL)) {
            ErgoSumParser sp = new ErgoSumParser(url);
            return sp.getPerson();
        } else if (!url.startsWith("file:")) {
            MicroformatDomParser sp = new MicroformatDomParser(url);
            return sp.getPerson();
        } else {
            return getVcardFile(url);
        }
    }

    public Contact addContact(String url, String alias) {
        if (url.matches("(\\w|-)+")) {
            return addContactBySpreadsheet(url, alias);
        } else if (!url.startsWith("file:")) {
            return addContactByWebsite(url);
        } else if (url.contains(ErgoSumParser.URL)) {
            return addContactByErgoSum(url);
        } else {
            return addContactByFile(Uri.parse(url));
        }
    }
}
